/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author ThanhTri
 */
public class PSboombubble extends PSystem {

    Vector vBoom;
    int Lenght;
    public PSboombubble(int _X,int _Y,Vector _vBoom,int _Lenght) throws IOException {
        super(RSParticle.getRS().getBubble());
        vBoom = _vBoom;
        m_XOrigin = _X*30;
        m_YOrigin = _Y*30;
        Lenght = _Lenght;
        
    }
    private boolean IsLive (Particle _particle){
        Rectangle rp = new Rectangle((int)_particle.m_X,
                (int) _particle.m_Y, 4, 4);
        Rectangle rm = new Rectangle ((int)m_XOrigin,(int)m_YOrigin,
                30,30);
        if (rm.Intersects(rp)){
            return true;
        }
        for (int i = 0; i < vBoom.size(); i++) {
            Point p = (Point)vBoom.elementAt(i);
            
            Rectangle rps = new Rectangle ((int)p.X*30,(int)p.Y*30,
                30,30);
            if (rps.Intersects(rp)){
                return true;
            }
        }
        return false;
    }
     public void Init (){
        m_MaxParticle = Lenght * 100 ;
        for (int i = 0;i<m_MaxParticle;i++){
		AddParticle();
	}
    }
     public void ResetParticle (Particle _particle){
        _particle.m_X = m_XOrigin + RamdomFloat(0, 30);
        _particle.m_Y = m_YOrigin + RamdomFloat(0, 30);
        
        _particle.m_Vx = 0 ;
        _particle.m_Vy = 0;
        switch(m_Ran.nextInt(4)){
            case 0:
                _particle.m_Vx = RamdomFloat(0, 400);  
                break;
            case 1:
                _particle.m_Vx = -1*RamdomFloat(0,400);
                break;
            case 2:
                _particle.m_Vy = RamdomFloat(0, 400);
                break;
            case 3:
                _particle.m_Vy = -1* RamdomFloat(0, 400);
                break;
                
        }
        
        _particle.m_LifeTime = 5;
        _particle.m_Age = 0;
        _particle.m_iLive = true;
    }
     public void Update (float _Time){
        if(!m_Particles.isEmpty()){
            for (int i = 0; i < m_Particles.size();) {
                Particle p = (Particle) m_Particles.elementAt(i);
                p.m_Y += p.m_Vy * _Time;
                p.m_X += p.m_Vx * _Time;
                if (!IsLive(p)){
                    m_Particles.removeElementAt(i);
                }else i++;
            }
        }
    }
      public void DrawScreen (Graphics _g,GameScreen _Screen){
     for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                    _g.drawRegion(m_Image, 0, 0, 4, 4, Sprite.TRANS_NONE,
                        (int)(p.m_X)- (_Screen.getXView() - _Screen.getWidth()/2),
                        (int)(p.m_Y)- (_Screen.getYView() - _Screen.getHeight()/2),Graphics.TOP|Graphics.LEFT);
            }
    }
      
}
